/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import view.VErro;
import view.VSucesso;
import model.MAutenticacao;

/**
 *
 * @author bruno
 */
public class DAutenticacao {

    public static boolean autentica(MAutenticacao aut) {
        try {
            Connection con = DCriaConexao.conexao();
            String query = "SELECT usuario, senha, nivel FROM autenticacao WHERE usuario = ?";
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1, aut.getUsuario());
            ResultSet rs; //Variável para receber os valores da consulta sql
            rs = stmt.executeQuery();
            while (rs.next()) {
                if (rs.getString("usuario").equals(aut.getUsuario()) && rs.getString("senha").equals(aut.getSenha())) {
                    stmt.close(); //Fechando comando sql
                    con.close(); //Fechando conexão 
                    return true;
                } else {
                    stmt.close(); //Fechando comando sql
                    con.close(); //Fechando conexão 
                    return false;
                }
            }
        } catch (SQLException ex) {
            VErro.erroBD(ex);
        }
        return false;
    }
}
